﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using MxWeiXinPF.DBUtility;
using MxWeiXinPF.Common;//Please add references
namespace MxWeiXinPF.DAL
{
    /// <summary>
    /// 数据访问类:wx_shop_coupon_data
    /// </summary>
    public partial class wx_shop_coupon_data
    {
        public wx_shop_coupon_data()
        { }
        #region  BasicMethod
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from wx_shop_coupon_data");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(MxWeiXinPF.Model.wx_shop_coupon_data model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into wx_shop_coupon_data(");
            strSql.Append("shopid,openid,gettype,status,usetime,gettime,senduid,ordersn,couponid,sendmsgstate,sendmsgtime)");
            strSql.Append(" values (");
            strSql.Append("@shopid,@openid,@gettype,@status,@usetime,@gettime,@senduid,@ordersn,@couponid,@sendmsgstate,@sendmsgtime)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@shopid", SqlDbType.Int,4),
					new SqlParameter("@openid", SqlDbType.VarChar,255),
					new SqlParameter("@gettype", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@usetime", SqlDbType.DateTime),
					new SqlParameter("@gettime", SqlDbType.DateTime),
					new SqlParameter("@senduid", SqlDbType.Int,4),
					new SqlParameter("@ordersn", SqlDbType.Int,4),
					new SqlParameter("@couponid", SqlDbType.Int,4),
					new SqlParameter("@sendmsgstate", SqlDbType.Int,4),
					new SqlParameter("@sendmsgtime", SqlDbType.DateTime)};
            parameters[0].Value = model.shopid;
            parameters[1].Value = model.openid;
            parameters[2].Value = model.gettype;
            parameters[3].Value = model.status;
            parameters[4].Value = model.usetime;
            parameters[5].Value = model.gettime;
            parameters[6].Value = model.senduid;
            parameters[7].Value = model.ordersn;
            parameters[8].Value = model.couponid;
            parameters[9].Value = model.sendmsgstate;
            parameters[10].Value = model.sendmsgtime;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(MxWeiXinPF.Model.wx_shop_coupon_data model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update wx_shop_coupon_data set ");
            strSql.Append("shopid=@shopid,");
            strSql.Append("openid=@openid,");
            strSql.Append("gettype=@gettype,");
            strSql.Append("status=@status,");
            strSql.Append("usetime=@usetime,");
            strSql.Append("gettime=@gettime,");
            strSql.Append("senduid=@senduid,");
            strSql.Append("ordersn=@ordersn,");
            strSql.Append("couponid=@couponid,");
            strSql.Append("sendmsgstate=@sendmsgstate,");
            strSql.Append("sendmsgtime=@sendmsgtime");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@shopid", SqlDbType.Int,4),
					new SqlParameter("@openid", SqlDbType.VarChar,255),
					new SqlParameter("@gettype", SqlDbType.Int,4),
					new SqlParameter("@status", SqlDbType.Int,4),
					new SqlParameter("@usetime", SqlDbType.DateTime),
					new SqlParameter("@gettime", SqlDbType.DateTime),
					new SqlParameter("@senduid", SqlDbType.Int,4),
					new SqlParameter("@ordersn", SqlDbType.Int,4),
					new SqlParameter("@couponid", SqlDbType.Int,4),
					new SqlParameter("@sendmsgstate", SqlDbType.Int,4),
					new SqlParameter("@sendmsgtime", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.shopid;
            parameters[1].Value = model.openid;
            parameters[2].Value = model.gettype;
            parameters[3].Value = model.status;
            parameters[4].Value = model.usetime;
            parameters[5].Value = model.gettime;
            parameters[6].Value = model.senduid;
            parameters[7].Value = model.ordersn;
            parameters[8].Value = model.couponid;
            parameters[9].Value = model.sendmsgstate;
            parameters[10].Value = model.sendmsgtime;
            parameters[11].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_coupon_data ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from wx_shop_coupon_data ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.wx_shop_coupon_data GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,shopid,openid,gettype,status,usetime,gettime,senduid,ordersn,couponid,sendmsgstate,sendmsgtime from wx_shop_coupon_data ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            MxWeiXinPF.Model.wx_shop_coupon_data model = new MxWeiXinPF.Model.wx_shop_coupon_data();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.wx_shop_coupon_data GetModel(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,shopid,openid,gettype,status,usetime,gettime,senduid,ordersn,couponid,sendmsgstate,sendmsgtime from wx_shop_coupon_data ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            MxWeiXinPF.Model.wx_shop_coupon_data model = new MxWeiXinPF.Model.wx_shop_coupon_data();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public MxWeiXinPF.Model.wx_shop_coupon_data DataRowToModel(DataRow row)
        {
            MxWeiXinPF.Model.wx_shop_coupon_data model = new MxWeiXinPF.Model.wx_shop_coupon_data();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["shopid"] != null && row["shopid"].ToString() != "")
                {
                    model.shopid = int.Parse(row["shopid"].ToString());
                }
                if (row["openid"] != null)
                {
                    model.openid = row["openid"].ToString();
                }
                if (row["gettype"] != null && row["gettype"].ToString() != "")
                {
                    model.gettype = int.Parse(row["gettype"].ToString());
                }
                if (row["status"] != null && row["status"].ToString() != "")
                {
                    model.status = int.Parse(row["status"].ToString());
                }
                if (row["usetime"] != null && row["usetime"].ToString() != "")
                {
                    model.usetime = DateTime.Parse(row["usetime"].ToString());
                }
                if (row["gettime"] != null && row["gettime"].ToString() != "")
                {
                    model.gettime = DateTime.Parse(row["gettime"].ToString());
                }
                if (row["senduid"] != null && row["senduid"].ToString() != "")
                {
                    model.senduid = int.Parse(row["senduid"].ToString());
                }
                if (row["ordersn"] != null && row["ordersn"].ToString() != "")
                {
                    model.ordersn = int.Parse(row["ordersn"].ToString());
                }
                if (row["couponid"] != null && row["couponid"].ToString() != "")
                {
                    model.couponid = int.Parse(row["couponid"].ToString());
                }
                if (row["sendmsgstate"] != null && row["sendmsgstate"].ToString() != "")
                {
                    model.sendmsgstate = int.Parse(row["sendmsgstate"].ToString());
                }
                if (row["sendmsgtime"] != null && row["sendmsgtime"].ToString() != "")
                {
                    model.sendmsgtime = DateTime.Parse(row["sendmsgtime"].ToString());
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,shopid,openid,gettype,status,usetime,gettime,senduid,ordersn,couponid,sendmsgstate,sendmsgtime ");
            strSql.Append(" FROM wx_shop_coupon_data ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,shopid,openid,gettype,status,usetime,gettime,senduid,ordersn,couponid,sendmsgstate,sendmsgtime ");
            strSql.Append(" FROM wx_shop_coupon_data ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM wx_shop_coupon_data ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from wx_shop_coupon_data T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }
        public DataSet GetList(int pageSize, int pageIndex, string strWhere, string filedOrder, out int recordCount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(string.Concat(new object[] { " select  * from wx_shop_coupon_data where 1=1 " }));
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            //if (filedOrder.Trim() != "")
            //{
            //    builder.Append(" order by " + filedOrder);
            //}
            recordCount = Convert.ToInt32(DbHelperSQL.GetSingle(PagingHelper.CreateCountingSql(builder.ToString())));
            return DbHelperSQL.Query(PagingHelper.CreatePagingSql(recordCount, pageSize, pageIndex, builder.ToString(), filedOrder));
        }

        public DataSet GetCouponByUidAndValid(int uid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT b.id as couponid,b.couponname,b.money,b.thumb,b.expiredthumb,b.usedthumb,b.timeend,b.status AS couponstate, a.status as usestate, a.id as dataid from wx_shop_coupon_data as a LEFT JOIN ");
            builder.Append("wx_shop_coupon as b on a.couponid = b.id  ");
            builder.Append(" where b.timeend > GetDate() and b.status =1 and a.status =0  and a.senduid = " + uid);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetCouponByUid(int uid)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT b.id as couponid,b.couponname,b.money,b.thumb,b.expiredthumb,b.usedthumb,b.timeend,b.status AS couponstate, a.status as usestate, a.id as dataid from wx_shop_coupon_data as a LEFT JOIN ");
            builder.Append("wx_shop_coupon as b on a.couponid = b.id  ");
            builder.Append(" where a.senduid = " + uid);
            return DbHelperSQL.Query(builder.ToString());
        }
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "wx_shop_coupon_data";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}

